
// Main header
#include "../Main.h"

// My header
#include "Controller_ModalColourPickerDialog.h"

// Fridge
#include "Controller_Main.h"
#include "../MathsHelper.h"
#include "../Widgets/DialogPanel.h"
#include "../Widgets/StaticText.h"
#include "../Widgets/TextEditor.h"
#include "../Widgets/Button.h"

static const u_int uDIALOG_BUTTON_HEIGHT = 32;
static const u_int uDIALOG_BUTTON_WIDTH = 128;
static const u_int uDIALOG_MARGIN = 24;

DialogPanel*			Controller_ModalColourPickerDialog::s_pxDialogPanel = 0;
Panel*					Controller_ModalColourPickerDialog::s_pxColourSwatch = 0;
TextEditor*				Controller_ModalColourPickerDialog::s_pxTextEditorRed = 0;
TextEditor*				Controller_ModalColourPickerDialog::s_pxTextEditorGreen = 0;
TextEditor*				Controller_ModalColourPickerDialog::s_pxTextEditorBlue = 0;
TextEditor*				Controller_ModalColourPickerDialog::s_pxTextEditorAlpha = 0;
bool					Controller_ModalColourPickerDialog::s_bDestroy = false;
u_int					Controller_ModalColourPickerDialog::s_uColour = 0;

void( * Controller_ModalColourPickerDialog::s_pfnCallback )( const Controller_ModalDialogResult, void* ) = NULL;
void* Controller_ModalColourPickerDialog::s_pCallbackUserData = NULL;

void Controller_ModalColourPickerDialog::Initialise()
{
}

void Controller_ModalColourPickerDialog::Update()
{
	if( s_bDestroy )
	{
		Destroy();
		return;
	}
	
	// Make sure our swatch matches what's in the text boxes
	if( s_pxDialogPanel )
	{
		u_int uRed = 0;
		u_int uGreen = 0;
		u_int uBlue = 0;
		u_int uAlpha = 0;
		
		sscanf( s_pxTextEditorRed->GetText(), "%u", &uRed );
		sscanf( s_pxTextEditorGreen->GetText(), "%u", &uGreen );
		sscanf( s_pxTextEditorBlue->GetText(), "%u", &uBlue );
		sscanf( s_pxTextEditorAlpha->GetText(), "%u", &uAlpha );
		
		s_uColour = 0;
		s_uColour |= ( uRed << 24 );
		s_uColour |= ( uGreen << 16 );
		s_uColour |= ( uBlue << 8 );
		s_uColour |= ( uAlpha );
		
		s_pxColourSwatch->SetColour( s_uColour );
	}
}

void Controller_ModalColourPickerDialog::Shutdown()
{
	Controller_Main::RemoveModal( s_pxDialogPanel );
	delete s_pxDialogPanel;
	s_pxDialogPanel = 0;
}

void Controller_ModalColourPickerDialog::Show()
{	
	if( s_pxDialogPanel )
	{
		return;
	}
	
	u_int uRed		= MathsHelper::Clamp( ( s_uColour >> 24 ) & 0xFF, 0u, 255u );
	u_int uGreen	= MathsHelper::Clamp( ( s_uColour >> 16 ) & 0xFF, 0u, 255u );
	u_int uBlue		= MathsHelper::Clamp( ( s_uColour >> 8 ) & 0xFF, 0u, 255u );
	u_int uAlpha	= MathsHelper::Clamp( ( s_uColour ) & 0xFF, 0u, 255u );
	
	char szTextRed[ 32 ];
	char szTextGreen[ 32 ];
	char szTextBlue[ 32 ];
	char szTextAlpha[ 32 ];
	
	sprintf( szTextRed, "%u", uRed );
	sprintf( szTextGreen, "%u", uGreen );
	sprintf( szTextBlue, "%u", uBlue );
	sprintf( szTextAlpha, "%u", uAlpha );
	
	s_pxDialogPanel = new DialogPanel( Controller_Main::GetWindowPanel() );
	Controller_Main::AddModal( s_pxDialogPanel );
	
	u_int uTextFieldHeight = 24;
	u_int uVerticalPos = s_pxDialogPanel->GetOriginalY() + uDIALOG_MARGIN;
	
	// Color swatch
	s_pxColourSwatch = new Panel( s_pxDialogPanel );
	s_pxColourSwatch->SetSize( 50, 50 );
	s_pxColourSwatch->SetPosition( s_pxDialogPanel->GetOriginalX() + uDIALOG_MARGIN / 2, s_pxDialogPanel->GetOriginalY() + uDIALOG_MARGIN );
	s_pxColourSwatch->SetHasBorder( true );
	s_pxColourSwatch->SetBorderWidth( 1.0f );
	s_pxColourSwatch->SetColour( s_uColour );
	
	// Text Editor - Red
	s_pxTextEditorRed = new TextEditor( s_pxDialogPanel );
	s_pxTextEditorRed->SetSize( 80, uTextFieldHeight );
	s_pxTextEditorRed->SetPosition( s_pxDialogPanel->GetOriginalX() + ( uDIALOG_MARGIN / 2 ) + 50 + uDIALOG_MARGIN , uVerticalPos );
	s_pxTextEditorRed->SetHasBorder( true );
	s_pxTextEditorRed->SetBorderWidth( 1.0f );
	s_pxTextEditorRed->SetSingleLine( true );
	s_pxTextEditorRed->SetNumberOnly( true );
	s_pxTextEditorRed->SetText( szTextRed );
	uVerticalPos += 34;
	
	// Text Editor - Blue
	s_pxTextEditorBlue = new TextEditor( s_pxDialogPanel );
	s_pxTextEditorBlue->SetSize( 80, uTextFieldHeight );
	s_pxTextEditorBlue->SetPosition( s_pxDialogPanel->GetOriginalX() + ( uDIALOG_MARGIN / 2 ) + 50 + uDIALOG_MARGIN, uVerticalPos );
	s_pxTextEditorBlue->SetHasBorder( true );
	s_pxTextEditorBlue->SetBorderWidth( 1.0f );
	s_pxTextEditorBlue->SetSingleLine( true );
	s_pxTextEditorBlue->SetNumberOnly( true );
	s_pxTextEditorBlue->SetText( szTextBlue );
	uVerticalPos += 34;
	
	// Text Editor - Green
	s_pxTextEditorGreen = new TextEditor( s_pxDialogPanel );
	s_pxTextEditorGreen->SetSize( 80, uTextFieldHeight );
	s_pxTextEditorGreen->SetPosition( ( s_pxDialogPanel->GetOriginalX() + uDIALOG_MARGIN / 2 ) + 50 + uDIALOG_MARGIN , uVerticalPos );
	s_pxTextEditorGreen->SetHasBorder( true );
	s_pxTextEditorGreen->SetBorderWidth( 1.0f );
	s_pxTextEditorGreen->SetSingleLine( true );
	s_pxTextEditorGreen->SetNumberOnly( true );
	s_pxTextEditorGreen->SetText( szTextGreen );
	uVerticalPos += 34;
	
	// Text Editor - Alpha
	s_pxTextEditorAlpha = new TextEditor( s_pxDialogPanel );
	s_pxTextEditorAlpha->SetSize( 80, uTextFieldHeight );
	s_pxTextEditorAlpha->SetPosition( ( s_pxDialogPanel->GetOriginalX() + uDIALOG_MARGIN / 2 ) + 50 + uDIALOG_MARGIN , uVerticalPos );
	s_pxTextEditorAlpha->SetHasBorder( true );
	s_pxTextEditorAlpha->SetBorderWidth( 1.0f );
	s_pxTextEditorAlpha->SetSingleLine( true );
	s_pxTextEditorAlpha->SetNumberOnly( true );
	s_pxTextEditorAlpha->SetText( szTextAlpha );
	
	// Dialog Panel
	s_pxDialogPanel->SetSize( 500, 300 );
    s_pxDialogPanel->SetPosition( MIN_SCREEN_WIDTH / 2 - s_pxDialogPanel->GetOriginalWidth() / 2, MIN_SCREEN_HEIGHT / 2 - s_pxDialogPanel->GetOriginalHeight() / 2  );
    s_pxDialogPanel->SetColour( 0xffffffff );
    s_pxDialogPanel->SetHasBorder( true );
	s_pxDialogPanel->SetHasMoveBar( true );
	
	// OK / Cancel Buttons
    Button* pxButton = NULL;
	
	pxButton = new Button( s_pxDialogPanel );
	pxButton->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
	pxButton->SetLabel( "OK" );
	pxButton->SetPosition( s_pxDialogPanel->GetOriginalX() + s_pxDialogPanel->GetOriginalWidth() / 2 - uDIALOG_BUTTON_WIDTH - uDIALOG_MARGIN / 2,
						  s_pxDialogPanel->GetOriginalY() + s_pxDialogPanel->GetOriginalHeight() - ( uDIALOG_MARGIN + uDIALOG_BUTTON_HEIGHT )
						  );
	pxButton->SetSize( uDIALOG_BUTTON_WIDTH, uDIALOG_BUTTON_HEIGHT );
	pxButton->SetButtonPressCallback( OKCallback, s_pCallbackUserData );
	
	pxButton = new Button( s_pxDialogPanel );
	pxButton->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
	pxButton->SetLabel( "Cancel" );
	pxButton->SetPosition( s_pxDialogPanel->GetOriginalX() + s_pxDialogPanel->GetOriginalWidth() / 2 + uDIALOG_MARGIN / 2,
						  s_pxDialogPanel->GetOriginalY() + s_pxDialogPanel->GetOriginalHeight() - ( uDIALOG_MARGIN + uDIALOG_BUTTON_HEIGHT )
						  );
	pxButton->SetSize( uDIALOG_BUTTON_WIDTH, uDIALOG_BUTTON_HEIGHT );
	pxButton->SetButtonPressCallback( CancelCallback, s_pCallbackUserData );
}

void Controller_ModalColourPickerDialog::OKCallback( void* pUserData )
{	
	s_bDestroy = true;
	
    if( s_pfnCallback )
    {
        s_pfnCallback( DIALOGRESULT_OK, pUserData );
    }
}

void Controller_ModalColourPickerDialog::CancelCallback( void* pUserData )
{		
    if( s_pfnCallback )
    {
		s_pfnCallback( DIALOGRESULT_CANCEL, pUserData );
    }
	
	s_bDestroy = true;
}

void Controller_ModalColourPickerDialog::Destroy()
{
	if( s_pxDialogPanel )
	{
		Controller_Main::RemoveModal( s_pxDialogPanel );
		delete s_pxDialogPanel;
		s_pxDialogPanel = 0;
	}
	s_bDestroy = false;
}

void Controller_ModalColourPickerDialog::SetCallback( void( * pfnCallback )( const Controller_ModalDialogResult, void* ), void* pCallbackUserData )
{
	s_pfnCallback = pfnCallback;
	s_pCallbackUserData = pCallbackUserData;
}


// eof
